package com.usian.admin.listener;

import com.alibaba.fastjson.JSON;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.vo.NewsVo;
import com.usian.utils.common.ScanImageUtil;
import com.usian.utils.common.ScanTextUtil;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author baimugudu
 * @email 2415621370@qq.com
 * @date 2023/8/30 15:14
 */
@Component
public class MyKafkaListener {

   @KafkaListener(topics={"usian-leadnews"})
    public void receviceMessage(ConsumerRecord<?,?> consumerRecord){
       String value = (String)consumerRecord.value();

       WmNews wmNews = JSON.parseObject(value, WmNews.class);
       String content = wmNews.getContent();

       List<NewsVo> newsVos = JSON.parseArray(content, NewsVo.class);
       newsVos.forEach(i->{
           String type = i.getType();
           if(type.equals("text")){
               try {
                   boolean b = ScanTextUtil.scanText(i.getValue());
                   if(b==false){

                   }
               } catch (Exception e) {
                   e.printStackTrace();
               }


           }

           if(type.equals("image")){
               try {
                   boolean b = ScanImageUtil.scanImage(i.getValue());
                   if(b==false){

                   }
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
       });



   }
}
